#include <vector>
#include <iostream>

using namespace std;

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return 0;
        }
        for (int i = 0; i < matrix.size(); i++) {
            for (int j = 0; j < matrix[0].size(); j++) {
                matrix[i][j] -= '0';
            }
        }
        for (int i = matrix.size() - 2; i >= 0; i--) {
            for (int j = matrix[0].size() - 2; j >= 0; j--) {
                if (matrix[i][j]) {
                    matrix[i][j] = max((int) matrix[i][j], min(min(matrix[i + 1][j], matrix[i][j + 1]), matrix[i + 1][j + 1]) + 1);
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < matrix.size(); i++) {
            for (int j = 0; j < matrix[0].size(); j++) {
                ans = max(ans, matrix[i][j] * matrix[i][j]);
            }
        }
        return ans;
    }
};

int main() {
    vector<vector<char>> input = {
            {'1', '0', '1', '0', '0'},
            {'1', '0', '1', '1', '1'},
            {'1', '1', '1', '1', '1'},
            {'1', '0', '0', '1', '0'},
    };
    Solution solution;
    int output = solution.maximalSquare(input);
    cout << output << endl;
    return 0;
}